package com.system.exception;

/**
 * 基础应用异常，系统内所有的自定义异常的父类
 * 
 * @author zhaoyu2
 * 
 */
public class ApplicationException extends Exception {
	private static final long serialVersionUID = -2647588658466977435L;

	private int errorCode;

	protected void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public ApplicationException(Throwable throwable) {
		super(throwable.toString());
		super.setStackTrace(throwable.getStackTrace());
		this.setErrorCode(ErrorCode.SystemError.UNKNOW_ERROR);
	}

	public ApplicationException(int errorCode, String message) {
		super(message == null ? "错误信息不存在" : message);
		this.setErrorCode(errorCode);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof ApplicationException))
			return false;
		final ApplicationException other = (ApplicationException) obj;
		if (this.getErrorCode() != other.getErrorCode())
			return false;
		return true;
	}

	@Override
	public String toString() {
		return new StringBuilder("[").append(this.getErrorCode()).append("]")
				.append(this.getMessage()).toString();
	}
}
